package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import com.example.repository.UserRepository;
import com.example.component.LoggerComponent;

@Configuration // 标识这是一个Spring配置类
@PropertySource("classpath:application.properties") // 加载属性文件（可选）
public class AppConfig {
    
    // 定义UserRepository Bean
    @Bean
    public UserRepository userRepository() {
        return new UserRepository();
    }
    
    // 定义LoggerComponent Bean
    @Bean
    public LoggerComponent loggerComponent() {
        LoggerComponent logger = new LoggerComponent();
        logger.setDebugMode(true); // 可以在这里设置属性
        return logger;
    }
    
    // 定义UserService Bean，并注入依赖
    @Bean
    public UserService userService(UserRepository userRepository, LoggerComponent loggerComponent) {
        // 通过方法参数自动注入依赖的Bean
        return new UserServiceImpl(userRepository, loggerComponent);
    }
    
    // 可以定义更多Bean...
    // @Bean
    // public AnotherService anotherService() {
    //     return new AnotherServiceImpl();
    // }
}